package com.ruoyi.wx;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.wx.domain.WxUser;
import com.ruoyi.common.wx.domain.vo.WxUserRegisterVo;
import com.ruoyi.common.wx.domain.vo.WxVo;
import com.ruoyi.common.wx.service.IWxService;
import com.ruoyi.common.wx.service.IWxUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@AllArgsConstructor
@RestController
@RequestMapping({"/app"})
public class AppUserController {
    @Autowired
    IWxUserService wxUserService;

//    @Autowired
//    IWxService wxService;
    private final IWxService wxService;

    @Autowired // 在较新的Spring版本中，这里的@Autowired可以省略
    public AppUserController(IWxService wxService) {
        this.wxService = wxService;
    }
    /**
     * <pre>
     * 微信授权获取openId,实现登录或注册
     * </pre>
     */
    @PostMapping("/loginByOpenId")
    public AjaxResult loginByOpenId(@RequestParam String openId) {
        try {
            return wxUserService.loginByOpenId(openId);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/getUserInfo")
    public AjaxResult getUserInfo(@RequestParam String token) {
        try {
            return wxUserService.getUserInfo(token);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/userTest")
    public AjaxResult apiTest(HttpServletRequest request) {
        try {
            Map map = new HashMap();
            // 获取属性值
            Map<String, Object> appUser = (Map<String, Object>)request.getAttribute("appUser");
            Long appUserId = (Long)request.getAttribute("appUserId");
            if (null != appUserId) {
                System.out.println(appUserId);
                map.put("appUserId", appUserId);
            }
            if (null != appUser) {
                WxUser user = BeanUtils.convertMapToObject(WxUser.class, appUser);
                map.put("appUser", user);
            }
            return AjaxResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * <pre>
     * 微信授权获取openId,实现登录或注册
     * </pre>
     */
    @PostMapping("/loginOrRegisterByOpenId")
    public AjaxResult loginOrRegisterByOpenId(@RequestBody WxVo input) {
        try {
            return AjaxResult.success(wxUserService.loginOrRegisterByOpenId(input.getJsCode()));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * <pre>
     * 微信授权获取openId
     * </pre>
     */
    @PostMapping("/getWxInfo")
    public AjaxResult getWxInfo(@RequestBody WxVo input) {
        try {
            return AjaxResult.success(wxService.getWxInfo(input.getJsCode()));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 登录 或者 注册 用户
     */
    @PostMapping("/loginOrRegister")
    public AjaxResult add(@RequestBody WxUserRegisterVo input) {
        try {
            String tokenRes = wxUserService.loginOrRegister(input);
            if (Constants.USER_NOT_EXIST.equals(tokenRes)) {
                return AjaxResult.error(Constants.USER_NOT_EXIST);
            } else {
                HashMap token = new HashMap<String, Object>(1) {{
                    put("token", tokenRes);
                }};
                return AjaxResult.success().put("data", token);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


}
